package com.ez.supervise.action.biz;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.ServletActionContext;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.OrgGoalSetting;
import com.ez.supervise.service.OrgGoalSettingService;
import com.ez.supervise.util.JsonUtils;

public class OrgGoalSettingAction extends BaseJsonAction {
   
    private static final long serialVersionUID = -5237037468195860016L;
    private OrgGoalSettingService orgGoalSettingService;
    private OrgGoalSetting orgGoalSetting;
    private Page page;
    private String orgIds;
    private String goalIds;
    private String goalType;
    

    public String saveOrgGoalSetting() {
	try {
	    orgGoalSettingService.save(orgIds,goalIds,goalType);
	    Result result = new Result();
	    outJsonString(JSONObject.fromObject(result).toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAllOrgGoalSetting() {
	try {
	    page = new Page();
	    List result = orgGoalSettingService.findAll();
	    page.setRoot(result);
	    page.setTotalProperty(result.size());
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }
    
    public String findOrgGoalSettingByPage() {
	try {
	    page = new Page();
	    page.putCondition("year", NumberUtils.isNumber(getRequest().getParameter("year")) ? Long.valueOf(getRequest().getParameter("year")) : null);
	    page.putCondition("orgId", NumberUtils.isNumber(getRequest().getParameter("orgId")) ? Long.valueOf(getRequest().getParameter("orgId")) : null);
//	    page.putCondition("auditResult", getRequest().getParameter("auditResult"));
//	    page.putCondition("type", getRequest().getParameter("type"));
//	    page.putCondition("content", getRequest().getParameter("content"));
		   
	    int start = NumberUtils.toInt(getRequest().getParameter("start"), 0);
	    int limit = NumberUtils.toInt(getRequest().getParameter("limit"), 20);
	    page.setStart(start++);
	    page.setLimit(limit);
	    page.addPageInfo();
	    orgGoalSettingService.findOrgGoalSettingByPage(page);
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }


    public String findAll() {
	try {
	    page = new Page();
	    List result = orgGoalSettingService.findAll();
	    page.setRoot(result);
	    page.setTotalProperty(result.size());
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     */
    public String deleteOrgGoalSetting() {
	try {
	    Long id = NumberUtils.isNumber(getRequest().getParameter("id")) ? Long.valueOf(getRequest().getParameter("id")) : null;
	    if (id != null && id > 0) {
		orgGoalSettingService.delete(id);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateOrgGoalSetting() throws Exception {
	String fieldName = getRequest().getParameter("fieldName");
	String fieldValue = getRequest().getParameter("fieldValue");
	String strOrgGoalSettingId = getRequest().getParameter("id");
	try {
//	    if (id != null && id > 0) {
//		OrgGoalSetting saveOrgGoalSetting = new OrgGoalSetting();
//		saveOrgGoalSetting.setId(id);
//		BeanUtils.setProperty(saveOrgGoalSetting, fieldName, fieldValue);
//		orgGoalSettingService.update(saveOrgGoalSetting);
//	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }
    

    public static void main(String[] args) {
	OrgGoalSetting orgGoalSetting = new OrgGoalSetting();
	orgGoalSetting.setId(1L);
	// orgGoalSetting.setName("linlinyu");
	JSONObject json = JSONObject.fromObject(orgGoalSetting);

	System.out.println(json);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(orgGoalSetting, config);
	System.out.println(jsonObj.toString());
    }

    public void setOrgGoalSettingService(OrgGoalSettingService orgGoalSettingService) {
	this.orgGoalSettingService = orgGoalSettingService;
    }

    public OrgGoalSetting getOrgGoalSetting() {
	return orgGoalSetting;
    }

    public void setOrgGoalSetting(OrgGoalSetting orgGoalSetting) {
	this.orgGoalSetting = orgGoalSetting;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

    public void setOrgIds(String orgIds) {
	this.orgIds = orgIds;
    }

    public String getOrgIds() {
	return orgIds;
    }

    public void setGoalIds(String goalIds) {
	this.goalIds = goalIds;
    }

    public String getGoalIds() {
	return goalIds;
    }

    public void setGoalType(String goalType) {
	this.goalType = goalType;
    }

    public String getGoalType() {
	return goalType;
    }

}
